package com.crm.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crm.domain.entity.Clue;
import com.crm.domain.query.ClueQuery;
import com.crm.mapper.ClueMapper;
import com.crm.service.IClueService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

@RequiredArgsConstructor
@Service
public class ClueServiceImpl extends ServiceImpl<ClueMapper, Clue> implements IClueService {

    private final ClueMapper clueMapper;

   /* *//**
     * 批量添加线索
     *//*
    @Override
    public void addBatch(MultipartFile file) throws IOException {
        List<Clue> list = getList(file);
        clueMapper.batchInsert(list);
    }*/
    /*private List<Clue> getList(MultipartFile file) throws IOException {
        //获取输入流并创建能读取Excel文件的工具
        ExcelReader reader = ExcelUtil.getReader(file.getInputStream());

        //把Excel文件的数据映射到user表里 并读取到List集合中
        List<Clue> clueList = reader.readAll(Clue.class);

        return clueList;
    }*/
    @Override
    public Page<Clue> queryPage(ClueQuery query) {
        // 1.构建条件
        // 1.1.分页条件
        Page<Clue> page = Page.of(query.getPageNum(),query.getPageSize());

        Map<String, Object> params = query.getParams();
        DateTimeFormatter dateTime = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        LocalDate beginCreateTime = ObjectUtil.isEmpty(params.get("beginCreateTime"))?null:(LocalDate.parse((String)params.get("beginCreateTime"), dateTime));
        LocalDate endCreateTime =ObjectUtil.isEmpty(params.get("endCreateTime"))?null:(LocalDate.parse((String)params.get("endCreateTime"), dateTime));
        //2 分页查询条件
        LambdaQueryWrapper<Clue> wrapper = Wrappers.<Clue>lambdaQuery()
                .eq(query.getId() != null, Clue::getId, query.getId())
                .like(query.getPhone() != null, Clue::getPhone, query.getPhone())
                .like(query.getOwner() != null, Clue::getOwner, query.getOwner())
                .eq(query.getChannel() != null, Clue::getChannel, query.getChannel())
                .eq(query.getStatus() != null, Clue::getStatus, query.getStatus())
                .between(ObjectUtil.isNotEmpty(beginCreateTime)&&ObjectUtil.isNotEmpty(endCreateTime)
                        , Clue::getCreateTime, beginCreateTime, endCreateTime);
       Page<Clue> cluePage = page(page, wrapper);

      return cluePage;
    }

    @Override
    public void importData(MultipartFile file) throws IOException {

        List<Clue> clueList = getFile(file);
        clueMapper.batchImportData(clueList);


    }

    private List<Clue> getFile(MultipartFile file) throws IOException {
        ExcelReader reader = ExcelUtil.getReader(file.getInputStream());
        return reader.readAll(Clue.class);
    }
}
